home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume2 / newest < prev    next >
Encoding:
Internet Message Format  |  1991-08-07  |  5.9 KB

  1. From: rick@pcrat.UUCP (Rick Richardson)
  2. Newsgroups: comp.sources.misc
  3. Subject: v02i027: newest - print file system summary
  4. Message-ID: <7152@ncoast.UUCP>
  5. Date: 30 Jan 88 21:26:36 GMT
  6. Approved: allbery@ncoast.UUCP
  7.  
  8. Comp.sources.misc: Volume 2, Issue 27
  9. Submitted-By: Rick Richardson <rick@pcrat.UUCP>
  10. Archive-Name: newest
  11.  
  12. We use this around here when cleaning up our disks.  Maybe
  13. others could use it, too.
  14.  
  15. -Rick Richardson
  16.  
  17. #! /bin/sh
  18. # This is a shell archive, meaning:
  19. # 1. Remove everything above the #! /bin/sh line.
  20. # 2. Save the resulting text in a file.
  21. # 3. Execute the file with /bin/sh (not csh) to create:
  22. #    newest.c
  23. # This archive created: Wed Jan 27 11:15:28 1988
  24. export PATH; PATH=/bin:/usr/bin:$PATH
  25. if test -f 'newest.c'
  26. then
  27.     echo shar: "will not over-write existing file 'newest.c'"
  28. else
  29. cat << \SHAR_EOF > 'newest.c'
  30. /*
  31.  *    newest:
  32.  *        Print file summary information.
  33.  *        Useful for cleaning up your disk space.
  34.  *
  35.  *    Donated to the public domain by Rick Richardson, 1988.
  36.  *    Use at your own risk. Quick & Dirty Coding Used Here.
  37.  *
  38.  *    Needs System V "ftw" routine or equivalent.
  39.  *    Needs System V "getopt" routine or equivalent.
  40.  *
  41.  *    No makefile needed, just type "make newest".
  42.  *
  43.  *    No man page needed, just type "newest" or "what newest".
  44.  *
  45.  *    Known to work on:
  46.  *        Venix SVR2.3    (80286)
  47.  *        UNIX SVR2    (VAX 785, 8700)
  48.  */
  49.  
  50. char    *Usage[] =
  51. {    /* Usage info, visible by 'what' */
  52. "@(#)    Usage:    newest [-st] directories ...",
  53. "@(#)    ",
  54. "@(#)        Print summary of access/modification/creation times",
  55. "@(#)        and/or file sizes of files found in directories",
  56. "@(#)    ",
  57. "@(#)        With no options, prints time information.",
  58. "@(#)        With -s, prints size information.",
  59. "@(#)        With -st, prints both type of information.",
  60. 0
  61. };
  62.  
  63. #include <stdio.h>
  64. #include <ftw.h>
  65. #include <sys/types.h>
  66. #include <sys/stat.h>
  67. #include <time.h>
  68.  
  69. int    unreadable;
  70. long    mtime;
  71. long    atime;
  72. long    Ctime;
  73. char    aname[BUFSIZ];
  74. char    mname[BUFSIZ];
  75. char    Cname[BUFSIZ];
  76. char    bigname[BUFSIZ];
  77. long    bigsize;
  78. int    files;
  79. long    bytes;
  80. int    bigflag;
  81. int    timeflag;
  82. #define    SECS    (60L*60L*24L)
  83. #define    EPOCH    9999999L
  84. long    dayvec[] =
  85. {
  86.     0, 1, 2, 3, 4, 5, 6, 7, 10, 20, 50, 100, 200, 500, EPOCH
  87. };
  88. #define    DAYSIZE    (sizeof(dayvec) / sizeof(dayvec[0]))
  89.  
  90. long    sizvec[] =
  91. {
  92.     0, 1000, 5000, 10000, 20000, 50000, 100000, 200000, 500000,
  93.     1000000, EPOCH
  94. };
  95. #define    SIZSIZE    (sizeof(sizvec) / sizeof(sizvec[0]))
  96.  
  97. int    afiles[DAYSIZE];
  98. int    mfiles[DAYSIZE];
  99. int    cfiles[DAYSIZE];
  100. int    sizfiles[SIZSIZE];
  101. long    now;
  102.  
  103. int
  104. in(mindays, maxdays, when)
  105. long    mindays, maxdays;
  106. long    when;
  107. {
  108.     long    minsecs = mindays * SECS;
  109.     long    maxsecs = maxdays * SECS;
  110.  
  111.     if ( (now-when)<maxsecs && (now-when)>=minsecs) return (1);
  112.     return (0);
  113. }
  114.  
  115. int
  116. siz(minsiz, maxsiz, thissiz)
  117. long    minsiz, maxsiz;
  118. long    thissiz;
  119. {
  120.     if ( thissiz<maxsiz && thissiz>=minsiz) return (1);
  121.     return (0);
  122. }
  123.  
  124. int
  125. fn(name, sp, what)
  126. char    *name;
  127. register struct stat    *sp;
  128. {
  129.     register int i;
  130.  
  131.     if (what != FTW_F && what != FTW_D)
  132.     {
  133.         ++unreadable;
  134.         return (0);
  135.     }
  136.     bytes += sp->st_size;
  137.     if (what != FTW_F) return(0);
  138.     ++files;
  139.     for (i = 1; i < DAYSIZE; ++i)
  140.     {
  141.         if (in(dayvec[i-1], dayvec[i], sp->st_mtime)) mfiles[i]++;
  142.         if (in(dayvec[i-1], dayvec[i], sp->st_atime)) afiles[i]++;
  143.         if (in(dayvec[i-1], dayvec[i], sp->st_ctime)) cfiles[i]++;
  144.     }
  145.     for (i = 1; i < SIZSIZE; ++i)
  146.         if (siz(sizvec[i-1], sizvec[i], sp->st_size)) sizfiles[i]++;
  147.     if (sp->st_mtime>mtime)
  148.         {mtime = sp->st_mtime; (void)strcpy(mname, name);}
  149.     if (sp->st_ctime>Ctime)
  150.         {Ctime = sp->st_ctime; (void)strcpy(Cname, name);}
  151.     if (sp->st_atime>atime)
  152.         {atime = sp->st_atime; (void)strcpy(aname, name);}
  153.     if (sp->st_size>bigsize)
  154.         {bigsize = sp->st_size; (void)strcpy(bigname, name);}
  155.     return (0);
  156. }
  157.  
  158. int
  159. main(argc, argv)
  160. int    argc;
  161. char    *argv[];
  162. {
  163.     char    *ctime();
  164.     long    time();
  165.     register int i, c, errflag = 0;
  166.     extern char    *optarg;
  167.     extern int    optind, opterr;
  168.  
  169.     while (( c = getopt(argc, argv, "st")) != EOF)
  170.         switch (c)
  171.         {
  172.         case 's':    bigflag = 1; break;
  173.         case 't':    timeflag = 1; break;
  174.         default:    errflag = 1; break;
  175.         }
  176.     if (optind == argc || errflag)
  177.     {
  178.         usage();
  179.         return (1);
  180.     }
  181.     now = time( (long *) 0);
  182.     for (i = optind; i < argc; ++i)
  183.     {
  184.         init();
  185.         walk(argv[i]);
  186.         stats(argv[i]);
  187.         graf(afiles, mfiles, cfiles);
  188.     }
  189.     return (0);
  190. }
  191.  
  192. init()
  193. {
  194.     register int    i;
  195.  
  196.     unreadable = 0;
  197.     mtime = 0;
  198.     atime = 0;
  199.     Ctime = 0;
  200.     bigsize = 0;
  201.     files = 0;
  202.     bytes = 0;
  203.     for (i = 1; i < DAYSIZE; ++i)
  204.     {
  205.         afiles[i] = 0;
  206.         mfiles[i] = 0;
  207.         cfiles[i] = 0;
  208.     }
  209.     for (i = 1; i < SIZSIZE; ++i)
  210.         sizfiles[i] = 0;
  211. }
  212.  
  213. walk(name)
  214. register char    *name;
  215. {
  216.     while (ftw(name, fn, 10) > 0)
  217.         ;
  218. }
  219.  
  220. stats(name)
  221. char    *name;
  222. {
  223.     (void)printf("\nDirectory:     %s\n", name);
  224.     (void)printf("Newest access: %s at %s", aname, ctime(&atime));
  225.     (void)printf("Newest modify: %s at %s", mname, ctime(&mtime));
  226.     (void)printf("Newest change: %s at %s", Cname, ctime(&Ctime));
  227.     (void)printf("Largest file:  %s is %ld bytes\n", bigname, bigsize);
  228.     if (unreadable)
  229.         (void) printf("Unreadable directories/files found\n");
  230. }
  231.  
  232. graf(avec, mvec, cvec)
  233. int    avec[];
  234. int    mvec[];
  235. int    cvec[];
  236. {
  237.     register int    i;
  238.  
  239.     if (!bigflag || timeflag)
  240.     {
  241.         for (i = 1; i < DAYSIZE; ++i)
  242.         {
  243.             if (dayvec[i] == EPOCH)
  244.                 (void) printf(
  245.         "%5ld - EPOCH days: %5d accessed %5d modified %5d changed\n",
  246.                     dayvec[i-1], avec[i], mvec[i], cvec[i]);
  247.             else
  248.                 (void) printf(
  249.         "%5ld - %5ld days: %5d accessed %5d modified %5d changed\n",
  250.                     dayvec[i-1], dayvec[i],
  251.                     avec[i], mvec[i], cvec[i] );
  252.         }
  253.         (void) printf(
  254.         "                    %5d TOTAL FILES, %7ld TOTAL BYTES\n\n",
  255.             files, bytes);
  256.     }
  257.  
  258.     if (bigflag)
  259.     {
  260.         for (i = 1; i < SIZSIZE; ++i)
  261.         {
  262.             if (sizvec[i] == EPOCH)
  263.                 (void)printf("%7ld -  ULIMIT bytes: %5d files\n",
  264.                     sizvec[i-1], sizfiles[i] );
  265.             else
  266.                 (void)printf("%7ld - %7ld bytes: %5d files\n",
  267.                     sizvec[i-1], sizvec[i], sizfiles[i] );
  268.         }
  269.         (void)printf("                    %7ld TOTAL BYTES\n", bytes);
  270.     }
  271. }
  272.  
  273. usage()
  274. {
  275.     register int    i;
  276.  
  277.     for (i = 0; Usage[i]; ++i)
  278.         (void)fprintf(stderr, "%s\n", &Usage[i][5]);
  279. }
  280. SHAR_EOF
  281. fi
  282. exit 0
  283. #    End of shell archive
  284.  
  285.  
  286.